package restlet.android;

import org.restlet.resource.ClientResource;
import java.util.ArrayList;
import com.cse210.grocerwe.client.*;

public class GrocerResource {
	private final String address = "http://grocerwe.appspot.com/android/interface";
	//private final String address = "http://10.0.2.2:8888/android/interface";
	private ClientResource cr;
    
    public GrocerResource(){}
    
    //Interface A
    public SItem[] getAllItems( String user )
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"A/"+user);
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceA resource = cr.wrap(APIInterfaceA.class);
    	
    	return resource.getAllItems();
    }
    public void addItem(SItem item)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"A/none");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceA resource = cr.wrap(APIInterfaceA.class);
    	resource.addItem(item);
    }
    
    //Interface B
    public SGroup[] getGroups(String user)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"B/"+user);
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceB resource = cr.wrap(APIInterfaceB.class);
    	return resource.getGroups();
    }
    
    public void editItem(SItem item)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"B/none");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceB resource = cr.wrap(APIInterfaceB.class);
    	resource.editItem(item);
    }
    
    //Interface C
    public SItem[] retrieveList(String group)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"C/"+group);
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceC resource = cr.wrap(APIInterfaceC.class);
    	return resource.retrieveList();
    }
    public void createList(String user,SGroup group)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"C/none");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceC resource = cr.wrap(APIInterfaceC.class);
    	resource.createList(user, group);
    }
    //Interface D
    public void invite(SGroup group, String user)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"D/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceD resource = cr.wrap(APIInterfaceD.class);
    	resource.invite(group, user);
    }
    //Interface E
    public void leaveGroup(String groupKey, String user)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"E/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceE resource = cr.wrap(APIInterfaceE.class);
    	resource.leaveGroup(groupKey, user);
    }
    //Interface F
    public void claimList(String groupKey, String user)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"F/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceF resource = cr.wrap(APIInterfaceF.class);
    	resource.claimList(null, user+"~"+groupKey);
    }
    //Interface G
    public void purchaseItems(ArrayList<SItem>items)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"G/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceG resource = cr.wrap(APIInterfaceG.class);
    	resource.purchaseItems(items);
    }
    //Interface H
    public void releaseList(String groupKey, String user)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"H/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceH resource = cr.wrap(APIInterfaceH.class);
    	resource.releaseList(groupKey, user+"~"+groupKey);
    }
    //Interface I
    public void removeItem(String itemKey)
    {
    	System.setProperty("java.net.preferIPv6Addresses", "false");
    	cr = new ClientResource(address+"I/");
    	cr.setRequestEntityBuffering(true);
    	APIInterfaceI resource = cr.wrap(APIInterfaceI.class);
    	resource.removeItem(itemKey);
    }
}
